using Protocol.Code;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class SetPanel : UIBase
{
    private void Awake()
    {

    }

    public override void Execute(int eventCode, object message)
    {
        switch (eventCode)
        {

            default:
                break;
        }
    }

    private Button btnSet;
    private Image imgBg;
    private Button btnClose;
    private Text txtAudio;
    private Toggle togAudio;
    private Text txtVolume;
    private Slider sldVolume;
    private Button btnQuit;

    void Start()
    {
        btnSet = transform.Find("btnSet").GetComponent<Button>();
        imgBg = transform.Find("imgBg").GetComponent<Image>();
        btnClose = transform.Find("btnClose").GetComponent<Button>();
        txtAudio = transform.Find("txtAudio").GetComponent<Text>();
        togAudio = transform.Find("togAudio").GetComponent<Toggle>();
        txtVolume = transform.Find("txtVolume").GetComponent<Text>();
        sldVolume = transform.Find("sldVolume").GetComponent<Slider>();
        btnQuit = transform.Find("btnQuit").GetComponent<Button>();

        // 初始状态
        btnSet.gameObject.SetActive(true);
        setObjectsActive(false);

        btnSet.onClick.AddListener(setClick);
        btnClose.onClick.AddListener(closeClick);
        btnQuit.onClick.AddListener(quitClick);
        togAudio.onValueChanged.AddListener(audioValueChanged);
        sldVolume.onValueChanged.AddListener(volumeValueChanged);
    }

    public override void OnDestroy()
    {
        base.OnDestroy();

        btnSet.onClick.RemoveListener(setClick);
        btnClose.onClick.RemoveListener(closeClick);
        btnQuit.onClick.RemoveListener(quitClick);
        togAudio.onValueChanged.RemoveListener(audioValueChanged);
        sldVolume.onValueChanged.RemoveListener(volumeValueChanged);

    }
    private void setObjectsActive(bool active)
    {
        imgBg.gameObject.SetActive(active);
        btnClose.gameObject.SetActive(active);
        txtAudio.gameObject.SetActive(active);
        togAudio.gameObject.SetActive(active);
        txtVolume.gameObject.SetActive(active);
        sldVolume.gameObject.SetActive(active);
        btnQuit.gameObject.SetActive(active);
    }

    private void setClick()
    {
        setObjectsActive(true);
    }
    private void closeClick()
    {
        setObjectsActive(false);
    }
    private void quitClick()
    {
        //Application.Quit();
        //Application.Quit();
        SceneManager.LoadScene(0);
        SocketMsg msg = new SocketMsg(OpCode.USER, UserCode.OFFLINE_CREQ, null);
        Dispatch(AreaCode.NET, 0, msg);
    }
    private void audioValueChanged(bool value)
    {
        // 操作声音
        if (value == true)
        {
            Dispatch(AreaCode.AUDIO, AudioEvent.PLAY_BG_AUDIO, null);
        }
        else
        {
            Dispatch(AreaCode.AUDIO, AudioEvent.STOP_BG_AUDIO, null);
        }
    }
    /// <summary>
    /// 滑动条滑动时调用
    /// </summary>
    /// <param name="result"></param>
    private void volumeValueChanged(float value)
    {
        // 操作声音
        Dispatch(AreaCode.AUDIO, AudioEvent.SET_BG_AUDIO, value);
    }

}
